/**
 * @author of2198
 * @date 2017/8/7.
 * @description 初始化openApi实例
 */
import React from "react";
import ReactDom from "react-dom";
import ReactDOMServer from "react-dom/server";
import Immutable from "immutable";
import { $http, getEnvXSite } from "x-site-util";
import xSiteOpenApi from "@qianmi/x-site-openapi";
import { getSystemCodeZip } from "./const-value";

//openApi实例
let xSiteInstance;
//系统编号压缩码
let systemCode;

const initXSite = async () => {
  let SystemCodeZip = getSystemCodeZip();
  //系统编号不一样时 销毁实例 重新声明openApi
  if (xSiteInstance && systemCode == SystemCodeZip) {
    return xSiteInstance;
  }

  systemCode = SystemCodeZip;
  //context参数暂时不传
  xSiteInstance = await xSiteOpenApi($http, {
    xsiteHost: getEnvXSite(),
    sc: SystemCodeZip,
    context: {
      ReactDom,
      React,
      ReactDOMServer,
      Immutable
    }
  });
  return xSiteInstance;
};

export default initXSite;
